'use strict';

app.controller('userManualGuideCtrl', ['$scope', '$timeout', '$http', '$stateParams', '$rootScope', '$modal', 'lodash', '$location', '$anchorScroll', function($scope, $timeout, $http, $stateParams, $rootScope, $modal, lodash, $location, $anchorScroll) {
	$rootScope.initBreadcrumb($scope.breadcrumb);
    $rootScope.initHeaderElements($scope.settings);
	var backUrl = {
        show : true,
        label: "返回",
        func : function() {
            $rootScope.backHistory();
        }
    };
    // $scope.settings.btnEle.push(addReqPrototype);
    $scope.settings.btnEle.push(backUrl);
    $scope.info={};

    setFlowHeight();

    function setFlowHeight() {
        $timeout(function() {        
            var h = document.documentElement.clientHeight || document.body.clientHeight;
            h = h - 155;
            $("#userManualGuide").children(".container-fluid").children(".row-tab").children("div:eq(1)").children("div.table-content").css({
                "max-height": h +"px",
                "overflow": "auto",
            });
        }, 500)
    }

    $(window).resize(function() {
        setFlowHeight();
    });

    // 添加用户（只能添加原型功能点）
    $scope.addPrototypeForReq = function() {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: getTemplateUrl("matrix/component/tpl/flowNodeModal.html"),
            controller: "addFeatureOrFlowCtrl",
            keyboard: true,
            backdrop: true,
            resolve: {
                data: function () {
                    return $scope.info.reqs;
                }
            }
        });

        modalInstance.result.then(function (featureItem) {
            relateForReq(featureItem);
        }, function (reason) {
            console.log("关闭modal");
        });
    }

    function relateForReq(featureItem) {
        var url = matrix_product_url +"/feature/featureOccupy?reqId="+ $stateParams.reqId +"&id="+ featureItem.id;
        $http.get(url).success(function(result) {
            if(result) {
                $scope.getReqAttr();
                $rootScope.toasterSuccessInfo("添加成功！");
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    }

    var url = matrix_product_url +"/brdRequirement/getBrdRequirements?withFeatureDesc=0&reqId="+ $stateParams.reqId;
    $http.get(url).success(function(result) {
        if(result.code == "0") {
            if(result.data) {
                $scope.reqDetails = result.data[0];
                $scope.breadcrumb.push('产品设计',$scope.reqDetails.reqName);
            }
        }
    });
    $scope.getReqAttr=function(){
        var reqUrl=matrix_product_url+"/feature/getAllFeatureForReq?reqId="+$stateParams.reqId+"&withAttr=1"
        $http.get(reqUrl).success(function(result){
            if(result.code == "0") {
                $scope.info.reqs=result.data;
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        })
    }
    $scope.getReqAttr();

    $scope.save=function(req){
        if(req.attr.url){
            var RegUrl = new RegExp(); 
            RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
            if (!RegUrl.test(req.attr.url)) { 
                $rootScope.toasterWarningInfo("请输入正确的url格式!");
                return false; 
            } 
        }
        req.reqId=$stateParams.reqId;
        $http({
            method: "post",
            url: matrix_product_url+"/feature/updateFeatureAttrForReq",
            data: JSON.stringify(req),
            headers: { "Content-Type": "application/json" }
        }).success(function(result) {
            if (result.code == "0") {
                $rootScope.toasterSuccessInfo("保存成功！");
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    }
}]);

app.controller("addFeatureOrFlowCtrl", ["$scope", "$stateParams", "$rootScope", "$modalInstance", "$http", "lodash", "data", function($scope, $stateParams, $rootScope, $modalInstance, $http, lodash, data) {
    $scope.flowTypeName = "添加功能点原型";
    $scope.isUserManualGuide = true;
    $scope.sysArray = config_flow_legend_sys;
    $scope.ffDataArray = [];

    // 切换系统
    $scope.switchSys = function(item) {
        $scope.sysItem = item;
        getAllFeature(item.crtname);
    }

    // 获取功能点数据(只显示原型数据)
    function getAllFeature(sys) {
        $("#showLoading").css("display", "block");
        $("#showList").css("display", "none");
        $scope.ffDataArray = new Array();
        var url = matrix_product_url +"/feature/getFeature?withPrototype=1&withAttr=0";
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                angular.forEach(result.data, function(data) {
                    data.crtname = (data.path || "") + "/" + (data.featureName || "");
                });
                var allFeature = new Array();
                angular.forEach(result.data, function(resData) {
                    if(sys) {
                        if(resData.sys == sys) {
                            if(resData.type == "PROTOTYPE") {
                                allFeature.push(resData);
                            }
                        }
                    }
                });
                angular.forEach(allFeature, function(feature) {
                    var hasFeatureIndex = lodash.findIndex(data, function(d) {
                        return d.id == feature.id;
                    });
                    if(hasFeatureIndex == -1) {
                        $scope.ffDataArray.push(feature);
                    }
                });
            } else {
                $rootScope.toasterErrorInfo("获取功能点数据错误！");
            }
            $("#showLoading").css("display", "none");
            $("#showList").css("display", "block");
        });
    }

    $scope.createClick = function () {
        if(!$scope.sysItem || !$scope.sysItem.crtname) {
            $rootScope.toasterWarningInfo("请选择系统类型！");
            return false;
        }
        if (!$scope.sltLegendItem) {
            $rootScope.toasterWarningInfo("添加数据不能为空！");
            return false;
        }
        $modalInstance.close($scope.sltLegendItem);
    };

    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);
